<%@ page import="com.tentact.coolmeetingtest.vo.Emp" %>
<%@ page import="java.util.List" %>
<%@ page import="com.tentact.coolmeetingtest.vo.Dept" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%--
  Created by IntelliJ IDEA.
  User: 40334
  Date: 2023/1/16
  Time: 16:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
  <link rel="stylesheet" href="styles/common.css" />
  <script src="js/jquery.js"></script>
  <script>
  var nowPage = 1;
  console.log('reload nowPage to 1');
  var totalPage = 0;
  var iPerPage = 10;
  var maxCount = 0;

  function checkAll(){
    setListInfo();
  }
  function setListInfo(){
    <%if(request.getAttribute("maxCount") != null){%>
      maxCount = <%=request.getAttribute("maxCount")%>;
      totalPage = Math.floor(maxCount / iPerPage) + (maxCount % iPerPage !== 0 ? 1 : 0);
    <%if(request.getParameter("new_search")!= null && request.getParameter("new_search").equals("yes")){%>
      nowPage = 1;
    <%}else {%>
      nowPage = <%=request.getParameter("now_page") == null ? 1 : request.getParameter("now_page")%>
    <%}%>;


      $("#max_count").text(maxCount);
      $("#total_page_disp").text(totalPage);
      $("#now_page_disp").text(nowPage);
      console.log('nowpage is' + nowPage);

    <%}%>
  }


    var url;
    function submitClick(){
      $("#now_page").val(nowPage);
      $("#i_per_page").val(10);
      $("#new_search").val('yes');
    }
    function prevPage(){
      if(nowPage > 1){
        nowPage--;
        getPage(nowPage);
      }
    }
    function getPage(page){
      if(page === -1 || page > totalPage){
          page = totalPage;
      }
      $("#new_search").val('');
      console.log('got now_page=' + page);
      if(page === 0){
        return;
      }
      window.location.href = "MainServlet?dest=emp.search&now_page=" + page + "&i_per_page=" + iPerPage;
      console.log('i am here,nowPage is ' + nowPage + " and req_now_page is " ${requestScope.now_page});
      setListInfo() ;
    }

    function nextPage(){
      if(nowPage < totalPage){
        nowPage++;
        getPage(nowPage);
        console.log(totalPage)
      }
    }

    function toPage(num){

      if(num <= 0 || num > totalPage){
        num = totalPage;
      }

      nowPage = num;
      getPage(nowPage);

    }
  </script>
</head>
<body onload="checkAll()">
  <div class="page-content">
    <div class="content-nav">
      会议预定 > 搜索员工
    </div>
    <form id="srchForm" action = "MainServlet">
      <input type="hidden" name="dest" value="emp.search">
      <input type="hidden" id="now_page" name="now_page" value="">
      <input type="hidden" id="i_per_page" name="i_per_page" value="">
      <input type="hidden" id="new_search" name="new_search" value="yes">
      <fieldset>
        <legend>搜索员工</legend>
        <table class="formtable">
          <tr>
            <td>姓名：</td>
            <td>
              <input type="text" id="employeename" name="emp_name" maxlength="20"/>
            </td>
            <td>账号名：</td>
            <td>
              <input type="text" id="accountname" name="username" maxlength="20"/>
            </td>
            <td>状态：</td>
            <td>
              <select name="state">
                <option value="">请选择</option>
                <option value="0">未审核</option>
                <option value="1">已通过</option>
                <option value="2">被拒绝</option>
              </select>
            </td>
          </tr>
          <tr>
            <td colspan="6" class="command">
              <input type="submit" onclick="submitClick()" class="clickbutton" value="查询"/>
              <input type="reset" class="clickbutton" value="重置"/>
            </td>
          </tr>
        </table>
      </fieldset>
    </form>
    <div>
      <h3 style="text-align:center;color:black">查询结果</h3>
      <div class="pager-header">
        <div class="header-info">
          共<span class="info-number" id="max_count">${maxCount}</span>条结果，
          分成<span class="info-number" id="total_page_disp">${maxCount div 10 + ( (maxCount mod 10) eq 0 ? 0 : 1)}</span>页显示，
          当前第<span class="info-number" id="now_page_disp">nowPage</span>页
        </div>
        <div class="header-nav">
          <input type="button" class="clickbutton" onclick="toPage(1)" value="首页"/>
          <input type="button" class="clickbutton" onclick="prevPage()" value="上页"/>
          <input type="button" class="clickbutton" onclick="nextPage()" value="下页"/>
          <input type="button" class="clickbutton" onclick="toPage(-1)" value="末页"/>
          跳到第<input type="text" id="pagenum" class="nav-number"/>页
          <input type="button" class="clickbutton" onclick="toPage($('#pagenum').val())"  value="跳转"/>
        </div>
      </div>
    </div>
    <table class="listtable">
      <tr class="listheader">
        <th>姓名</th>
        <th>账号名</th>
        <th>联系电话</th>
        <th>电子邮件</th>
        <th>操作</th>
      </tr>
      <%
        List< Emp > empList = (List<Emp>) request.getAttribute("emps");
        if(empList != null){
          for(Emp emp : empList){
      %>
      <tr>
        <td><%=emp.getEmpName()%></td>
        <td><%=emp.getUsername()%></td>
        <td><%=emp.getTel()%></td>
        <td><%=emp.getEmail()%></td>
        <td><a class="clickbutton" href="MainServlet?dest=emp.closeUser&empno=<%=emp.getEmpno()%>&state=0">关闭账号</a></td>
      </tr>
        <%}
        }%>
    </table>
  </div>
</body>
</html>
